<template>
  <div class="about">
    <div class="top">
      <van-nav-bar
        title="列表页"
        left-text="返回"
        left-arrow
        @click-left="onClickLeft"
      ></van-nav-bar>
      <van-dropdown-menu>
        <van-dropdown-item />
        <van-dropdown-item title="筛选" ref="item">
          <van-cell center title="包邮">
            <template #right-icon>
              <van-switch size="24" active-color="#ee0a24" />
            </template>
          </van-cell>
          <van-cell center title="团购">
            <template #right-icon>
              <van-switch size="24" active-color="#ee0a24" />
            </template>
          </van-cell>
          <div style="padding: 5px 16px">
            <van-button type="danger" block round @click="onConfirm">
              确认
            </van-button>
          </div>
        </van-dropdown-item>
      </van-dropdown-menu>
    </div>
    <div class="main">
      <div class="side">
        <van-sidebar v-model="activeKey" @change="onChange">
          <van-sidebar-item
            v-for="item in categori"
            :key="item.id"
            :title="item.name"
            :badge="item.total"
            @click="getgate(item.id)"
          />
        </van-sidebar>
      </div>
      <div class="cards">
        <van-card
          v-for="item in catelist"
          :key="item.id"
          :num="item.amount"
          :price="item.price"
          :desc="item.desc"
          :title="item.name"
          :thumb="item.coverImage | diaImg"
          @click="loadetail(item.id)"
        >
          <template #tags>
            <van-tag plain type="danger">标签</van-tag>
            <van-tag plain type="danger">标签</van-tag>
          </template>
          <template #footer>
            <van-button size="mini">按钮</van-button>
            <van-button size="mini">按钮</van-button>
          </template>
        </van-card>
      </div>
    </div>
  </div>
</template>
<script>
import { Notify } from "vant";
import { loadCategoriesAPI } from "@/services/categories";
import { loadCategoriListAPI } from "@/services/catgarilist";
export default {
  name: "AboutView",
  data() {
    return {
      // 商品分类
      categori: [],
      // 分类商品数组
      catelist: [],
      activeKey: 0,
      defaultId:0,
    };
  },
  components: {},
  created() {
     this.loadData();
  },
  methods: {
    async loadData() {
      // 获取分类商品
      const categori = await loadCategoriesAPI();
      const allProduct = [];
      categori.data.forEach((item) => {
        allProduct.push(loadCategoriListAPI(item.id));
      });
      Promise.all(allProduct).then((res) => {
        this.categori = categori.data.map((item, index) => {
          return { ...item, total: res[index].total};
        });
         this.defaultId = this.categori[0].id;
         this.getgate(this.$route.query.id?this.$route.query.id:this.defaultId);
      });
    },
    onConfirm() {},
    onClickLeft() {
      this.$router.push({
        name: "Home",
      });
    },
    onChange(index) {
      console.log(index);
      Notify({ type: "primary", message: index });
  
    },
    async getgate(id) {
      const caris = await loadCategoriListAPI(id);
      this.catelist = caris.data;
    },
    loadetail(id) {
      this.$router.push({
        name: "Detail",
        params: {
          id,
        },
      });
    },
  },
};
</script>

<style scoped>
.about {
  display: flex;
  flex-direction: column;
}
.top {
  height: 80px;
}
.main {
  display: flex;
  flex: 1;
}
.side {
  width: 25vw;
}

.cards {
  overflow: auto;
  flex: 1;
}
</style>
